package com.flash.dataU.test.controller;

import com.flash.dataU.test.entity.Project;
import com.flash.dataU.test.entity.ProjectType;
import com.flash.dataU.test.entity.Server;
import com.flash.dataU.test.service.impl.ProjectService;
import com.flash.dataU.test.service.impl.ProjectTypeService;
import com.flash.dataU.test.service.impl.ServerService;
import com.flash.dataU.utils.HttpUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.applet.Main;

/**
 * .
 *
 * @author sunym1993
 * @version 0.0.1-SNAPSHOT
 * @since 2017年12月06日 17时34分
 */

@Controller
@RequestMapping("project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectTypeService projectTypeService;

    @Autowired
    private ServerService serverService;

    public static void main(String[] args) throws Exception {
        Map map = new HashMap();
        map.put("message","123");
        HttpResponse response = HttpUtils.doPost("http://smsback.credithc.com/sms-back/sms/send",  null, null, null, map);
        System.out.println(response.getEntity());

        HttpEntity entity = response.getEntity();

        InputStream content = entity.getContent();
        String res = convert(content);
        System.out.println(res);

    }

    public static String convert(InputStream in) throws UnsupportedEncodingException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        return sb.toString();
    }

    @RequestMapping("checkUrl")
    @ResponseBody
    public String checkUrl(String url){
        try {
            // 测试GET请求
            HttpResponse response = HttpUtils.doGet(url, null, null, null);

            if (!parseStatus(response).equals("0")) {
                return parseStatus(response);
            }

            if (response.getStatusLine().getStatusCode() == 405) {
                // 测试POST请求
                response  = HttpUtils.doPost(url, null, null, null,null);
                if (!parseStatus(response).equals("0")) {
                    return parseStatus(response);
                }
                return response.getStatusLine().getStatusCode()+"";
            }

            return response.getStatusLine().getStatusCode()+"";
        }
        catch (Exception e) {
            int start = url.indexOf("://")+3;
            int end = url.lastIndexOf(":");
            String ip = url.substring(start, end);

            url = url.substring(end+1);
            int port;
            try {
                port = Integer.parseInt(url.substring(0, url.indexOf("/")));
            } catch (NumberFormatException nume) {
                return "请输入端口号再测试";
            }

            try {
                Socket socket = new Socket(ip, port);
            }
            catch (IOException e1) {
                return e.getMessage();
            }

            return "dubbo访问成功";
        }
    }

    @RequestMapping("findAllList")
    public String findAllList(HttpServletRequest request){

        List<Project> projects = projectService.findAllList();

        for (Project project : projects) {
            int typeId = project.getType().getId();
            ProjectType projectType = projectTypeService.get(typeId);
            project.setType(projectType);

            List<Server> servers = serverService.findByProjectId(project.getId());
            project.setServers(servers);
        }

        request.setAttribute("projects", projects);

        return "project";
    }

    @RequestMapping("findAllListShowServers")
    public String findAllListShowServers(int serverTypeId, HttpServletRequest request){

        List<Project> projects = projectService.findAllList();

        for (Project project : projects) {
            int typeId = project.getType().getId();
            ProjectType projectType = projectTypeService.get(typeId);
            project.setType(projectType);

            List<Server> servers = serverService.findByProjectId(project.getId());
            servers.removeIf(server -> server.getType().getId() != serverTypeId);
            project.setServers(servers);
        }

        request.setAttribute("projects", projects);

        return "project";
    }

    private String parseStatus(HttpResponse response){
        int status = response.getStatusLine().getStatusCode();
        switch (status) {
            case 400:
                return "web访问成功--400";
            case 404:
                return "404";
            default:
                return "0";
        }

    }

}
